import { Body, Controller, Post,  Inject } from '@nestjs/common';
import { AppService } from './app.service';
import Utils from 'src/utils';
import {AuthUser} from '../../decorator'
import User from './user.entity';

@Controller()
export class AppController {
  @Inject()
  private appService: AppService
  @Inject()
  private readonly utils: Utils;

  @Post('chat')
  proxyChat(@Body() { prompt }): any {
    return this.appService.proxyChat(prompt)
  }

  @Post('login')
  async onLogin(@Body('code') js_code: string): Promise<any> {
    const token = await this.utils.genToken(js_code);
    return { token };
  }

  @Post('user')
  async userInfo(@AuthUser() user) {
    const data = await this.appService.findOne(user.openid);
    return { data };
  }
  
  @Post('saveUser')
  async saveUser(@Body() user: User, @AuthUser() loginUser) {
    console.log(user, loginUser);
    const data = await this.appService.saveUser({...user, id: loginUser.openid});
    return { data };
  } 
}
